import { getKnowledgeList } from '@/services';
import { IKnowledgeItem } from '@/types';
import { Effect,  Reducer,  } from 'umi';

export interface KnowledgeModelState {
    knowledgeList:IKnowledgeItem[],
    knowledgeLength:number
}

export interface KnowledgeModelType {
  namespace: 'knowledge';
  state: KnowledgeModelState;
  effects: {
    getKnowledgeList: Effect;
  };
  reducers: {
    save: Reducer<KnowledgeModelState>;
  };
}

const KnowledgeModel: KnowledgeModelType = {
  namespace: 'knowledge',

  state: {
    knowledgeLength:0,
    knowledgeList:[]
  },

  effects: {
    *getKnowledgeList({payload},{call,put}) {
        const result = yield call(getKnowledgeList);
        if(result.data) {
            yield put({
              type:'save',
              payload:{
                knowledgeList:result.data[0],
                knowledgeLength:result.data[1]
              }
            })
          }
    }
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};

export default KnowledgeModel;